package com.wrupple.muba.catalogs.client.widgets.fields.cells;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.widgets.fields.cells.templates.RGBColorOutput;
import com.wrupple.muba.catalogs.client.widgets.fields.cells.templates.TextInput;
import com.wrupple.muba.common.shared.SequentialProcess;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.muba.widget.client.widgets.colorPicker.ColorPicker;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class ColorPickerFieldCell extends AbstractDelegatingEditableField<String> {
	TextInput input;

	RGBColorOutput output;

	public ColorPickerFieldCell(EventBus bus,ProcessManager pm,FieldDescriptor d, Mode mode) {
		super(bus, pm, d, mode);
		input = GWT.create(TextInput.class);
		output = GWT.create(RGBColorOutput.class);
	}

	@Override
	protected void renderAsInput(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb,
			AbstractEditableField.FieldData<String> viewData) {
		if (value == null) {
			sb.append(input.input(""));
		} else {
			sb.append(input.input(value));
		}
	}

	@Override
	protected void renderReadOnly(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb,
			com.wrupple.muba.catalogs.client.widgets.fields.cells.AbstractEditableField.FieldData<String> viewData) {
		if (value != null) {
			sb.append(output.defaultCatalogFileInput(value));
		}
	}

	@Override
	protected String getCurrentInputValue(Element parent,  boolean isEditing) {
		String value = null;
		if (isEditing) {
			value = parent.getFirstChildElement().getAttribute("value");
		} else {
			value = parent.getFirstChildElement().getFirstChildElement().getInnerText();
		}
		return value;
	}

	@Override
	protected String getProcessLocalizedName() {
		return "Color�";
	}

	@Override
	protected SequentialProcess<String, String> getDelegateProcess() {
		ColorPicker picker = new ColorPicker();
		return SequentialProcess.wrap(picker, picker, getProcessLocalizedName());
	}


}
